<!--
 * @: 节流
 * @Version: 1.0
 * @Autor: ziwei
 * @Date: 2021-10-13 13:54:13
 * @LastEditors: ziwei
 * @LastEditTime: 2021-10-15 14:28:37
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div {
      background: yellow;
      color: red;
      width: 100px;
      height: 40px;
      border: 1px solid red;
    }
  </style>
</head>

<body>
  <div id="btn">点击</div>
  <script>
    //理论上使用完的对象要重置为null
    function debounce(func, wait, immediate)
    {
      let timer;
      return function ()
      {
        if (!timer&&immediate) {
          func.apply(this, arguments);
        }
        clearTimeout(timer)
        timer = setTimeout(() =>
        {
          console.log('执行');
          func.apply(this, arguments);
          timer=null
        }, wait);
      }
    }

    function logger()
    {
      console.log('logger点击点击点击');
    }
    //第三个参数表示首次触发
    btn.addEventListener('click', debounce(logger, 1000, true))
  </script>
</body>

</html>